#!/usr/bin/python
from matplotlib.pylab import *

def averages(filename):
	xt=[]; xm=[];
	g11m=[]; g22m=[]; g12m=[];
	g11d=[]; g22d=[]; g12d=[];
	d=load(filename)
	n1=d[:,0]; n2=d[:,1]
	g11=d[:,9]; g22=d[:,10]; g12=d[:,11]
	x=n1/(n1+n2)
	
	x0=x[0]
	xt=[]; g11t=[]; g22t=[]; g12t=[];
	xt.append(x[0])
	g11t.append(g11[0])
	g22t.append(g22[0])
	g12t.append(g12[0])
	
	#print x[0],g11[0]
	for i in range(1,len(x)):
		if x[i]==x0:
			#print x[i],g11[i]
			xt.append(x[i])
			g11t.append(g11[i])
			g22t.append(g22[i])
			g12t.append(g12[i])
		else:
			xm.append(mean(xt))
			g11m.append(mean(g11t))
			g22m.append(mean(g22t))
			g12m.append(mean(g12t))
			g11d.append(std(g11t))
			g22d.append(std(g22t))
			g12d.append(std(g12t))
			
			x0=x[i]		
			xt=[]; g11t=[]; g22t=[]; g12t=[];
			#print x[i],g11[i]		
			xt.append(x[i])
			g11t.append(g11[i])
			g22t.append(g22[i])
			g12t.append(g12[i])	
	
	xm.append(mean(xt))
	g11m.append(mean(g11t))
	g22m.append(mean(g22t))
	g12m.append(mean(g12t))
	g11d.append(std(g11t))
	g22d.append(std(g22t))
	g12d.append(std(g12t))
	out=[xm,g11m,g22m,g12m,g11d,g22d,g12d]
	return out


def graph(system,level):
	experiment=system+"-exp.txt"
	ff1=system+"-sff.txt"
	ff2=system+"-skb.txt"
	
	exp=load(experiment)
	plot(exp[:,0],exp[:,4],label='G11-exp',color='black')
	hold(True)
	title(system)
	plot(exp[:,0],exp[:,5],label='G22-exp',color='red')
	plot(exp[:,0],exp[:,6],label='G12-exp',color='blue')
	
	if level==1 or level==4:
		out=averages(ff1)
		errorbar(out[0],out[1],out[4],label='G11-Siepmann',fmt='o',markerfacecolor='black',markeredgecolor='black',ecolor='black',linestyle='None')
		errorbar(out[0],out[2],out[5],label='G22-Siepmann',fmt='o',markerfacecolor='red',markeredgecolor='red',ecolor='red',linestyle='None')
		errorbar(out[0],out[3],out[6],label='G12-Siepmann',fmt='o',markerfacecolor='blue',markeredgecolor='blue',ecolor='blue',linestyle='None')
		
	if level==3 or level==4:
		out=averages(ff2)
		errorbar(out[0],out[1],out[4],label='G11-KB',fmt='o',markerfacecolor='white',markeredgecolor='black',ecolor='black',linestyle='None')
		errorbar(out[0],out[2],out[5],label='G22-KB',fmt='o',markerfacecolor='white',markeredgecolor='red',ecolor='red',linestyle='None')
		errorbar(out[0],out[3],out[6],label='G12-KB',fmt='o',markerfacecolor='white',markeredgecolor='blue',ecolor='blue',linestyle='None')
		
	legend(loc=2)

graph('mshmoh',4)
savefig('mshmoh-i.png')
figure()
graph('msmmoh',4)
savefig('msmmoh-i.png')
show()	
